package com.example.dw.admin.service.impl;

import com.example.dw.admin.dto.AdminLoginDTO;
import com.example.dw.admin.dto.AdminUpdateUserDTO;
import com.example.dw.admin.dto.AdminUpdatePasswordDTO;
import com.example.dw.admin.service.AdminAuthService;
import com.example.dw.admin.entity.AdminUser;
import com.example.dw.admin.mapper.AdminUserMapper;
import com.example.dw.entity.User;
import com.example.dw.utils.AdminJwtUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * 管理端认证服务实现类
 * @author system
 * @since 2024-01-01
 */
@Slf4j
@Service
public class AdminAuthServiceImpl implements AdminAuthService {

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private AdminJwtUtil adminJwtUtil;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public Map<String, Object> login(AdminLoginDTO loginDTO) {
        // 查询管理员用户
        QueryWrapper<AdminUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", loginDTO.getUsername());
        
        AdminUser admin = adminUserMapper.selectOne(queryWrapper);
        
        if (admin == null) {
            throw new RuntimeException("管理员不存在");
        }
        
        // 验证密码
        if (!passwordEncoder.matches(loginDTO.getPassword(), admin.getPassword())) {
            throw new RuntimeException("密码错误");
        }
        
        // 生成管理端Token
        String token = adminJwtUtil.generateAdminToken(
            admin.getId(), 
            admin.getUsername(), 
            "ADMIN"
        );
        
        Map<String, Object> result = new HashMap<>();
        result.put("token", token);
        result.put("adminInfo", admin);
        
        return result;
    }

    @Override
    public AdminUser getAdminInfo(Integer adminId) {
        AdminUser admin = adminUserMapper.selectById(adminId);
        if (admin == null) {
            throw new RuntimeException("管理员不存在");
        }
        return admin;
    }

    @Override
    public void updateAdminInfo(AdminUpdateUserDTO updateDTO) {
        AdminUser admin = adminUserMapper.selectById(updateDTO.getUserid());
        if (admin == null) {
            throw new RuntimeException("管理员不存在");
        }
        
        // 更新管理员信息
        AdminUser updateAdmin = new AdminUser();
        updateAdmin.setId(updateDTO.getUserid());
        updateAdmin.setNickname(updateDTO.getNickname());
        updateAdmin.setPhone(updateDTO.getPhone());
        updateAdmin.setEmail(updateDTO.getEmail());
        updateAdmin.setSex(updateDTO.getGender() != null ? updateDTO.getGender().toString() : null);
        updateAdmin.setPic(updateDTO.getAvatar());
        updateAdmin.setUpdatedAt(LocalDateTime.now());
        
        adminUserMapper.updateById(updateAdmin);
    }

    @Override
    public void updateAdminPassword(AdminUpdatePasswordDTO updatePasswordDTO) {
        AdminUser admin = adminUserMapper.selectById(updatePasswordDTO.getUserid());
        if (admin == null) {
            throw new RuntimeException("管理员不存在");
        }
        
        // 验证旧密码
        if (!passwordEncoder.matches(updatePasswordDTO.getOldPassword(), admin.getPassword())) {
            throw new RuntimeException("旧密码错误");
        }
        
        // 更新密码
        AdminUser updateAdmin = new AdminUser();
        updateAdmin.setId(updatePasswordDTO.getUserid());
        updateAdmin.setPassword(passwordEncoder.encode(updatePasswordDTO.getNewPassword()));
        updateAdmin.setUpdatedAt(LocalDateTime.now());
        
        adminUserMapper.updateById(updateAdmin);
    }
} 